<html>
 <head>
  <title>Test Page for Component Communications</title>
  <script language="javascript" src="../../jsunit/app/jsUnitCore.js"></script>
  <script language="javascript" src="../compiled/ca-all.js"></script>
 </head>
 <body>
  <script language="javascript">
  	var _counter = 0;
	var TComponentPartail = function(groups){
		this.groups = groups || [];
	};		
	TComponentPartail.prototype.InitializeComponent = function(component_manager){
		for (var i = 0; i < this.groups.length; i++) {
			CA.ComponentCommunications.RegisterForBroadcast(this, this.groups[i]);
		}
	};
	TComponentPartail.prototype.SetComponentID = function(cmpid){
		this.cid = cmpid;
	};
	TComponentPartail.prototype.GetComponentID = function(){
		return this.cid;
	};
	var TComponent = function(groups){
		TComponentPartail.call(this,groups);
	};
	TComponent.prototype = new TComponentPartail();
	TComponent.prototype.OnBroadcast = function(evt,fn){
		fn(this.cid);
	};
	TComponent.prototype.Add = function(x,y,token){
		token.value = x+y;
	};
  	function setUp(){
		var uids = [];
		for(var i=0;i<5;i++){
			uids.push("UID_"+_counter);
			_counter++;
		}	
		CA.UID.DefaultProvider.AddUIDS(uids);	
	}
    function testCreation() {
		var oPartial = new TComponentPartail(['fail']);
	    var success = false;
		try{
			CA.Components.Register(oPartial);
		}catch(e){
			success = true;
		}
		assertTrue("Should fail due to registering for group",success);
		var o = new TComponent(["fail"]);
		success = true;
		try{
			CA.Components.Register(o);
		}catch(e){
			alert(e);
			success = false;
		}
		assertTrue("Should pass due to implementing broadcast interface",success);
    }
    function testSendMessage() {
		var o = new TComponent(["fail"]);
		success = true;
		try{
			CA.Components.Register(o);
		}catch(e){
			success = false;
		}
		assertTrue("Should pass due to implementing broadcast interface",success);
		var token = {};
		CA.ComponentCommunications.SendMessage(o.GetComponentID(),"Add",2,1,token);
		assertEquals("Should get 3 back",3,token.value);
    }
    function testBroadcast() {
		var o = new TComponent(["retval"]);
		success = true;
		try{
			CA.Components.Register(o);
		}catch(e){
			success = false;
		}
		assertTrue("Should pass due to implementing broadcast interface",success);
		var rCID = null;
		function test(ncid){
			rCID = ncid;
		}
		CA.ComponentCommunications.BroadcastMessage("retval",test);
		assertEquals("IDs should equal",o.GetComponentID(),rCID);
    }
    function testUnregisterBroadcast() {
		var o = new TComponent(["retval"]);
		success = true;
		try{
			CA.Components.Register(o);
		}catch(e){
			success = false;
		}
		assertTrue("Should pass due to implementing broadcast interface",success);
		var rCID = null;
		function test(ncid){
			rCID = ncid;
		}
		CA.ComponentCommunications.BroadcastMessage("retval",test);
		assertEquals("IDs should equal",o.GetComponentID(),rCID);
		CA.ComponentCommunications.UnregisterForBroadcast(o,"retval");
		rCID = null;
		CA.ComponentCommunications.BroadcastMessage("retval",test);
		assertNotEquals("IDs should not be equal",o.GetComponentID(),rCID);
    }
    function testConnectSpecific() {
		var o = new TComponent(["retval"]);
		success = true;
		try{
			CA.Components.Register(o);
		}catch(e){
			success = false;
		}
		assertTrue("Should pass due to implementing broadcast interface",success);
		var rCID = null;
		function test(ncid){
			rCID = ncid;
		}
		CA.ComponentCommunications.Connect(o,"event",test,null);
		CA.ComponentCommunications.FireEvent(o,"event",o.GetComponentID());
		assertEquals("IDs should equal",o.GetComponentID(),rCID);
    }
    function testConnectGeneral() {
		var o = new TComponent(["retval"]);
		success = true;
		try{
			CA.Components.Register(o);
		}catch(e){
			success = false;
		}
		assertTrue("Should pass due to implementing broadcast interface",success);
		var rCID = null;
		function test(ncid){
			rCID = ncid;
		}
		CA.ComponentCommunications.Connect(null,"event2",test,null);
		CA.ComponentCommunications.FireEvent(o,"event2",o.GetComponentID());
		assertEquals("IDs should equal",o.GetComponentID(),rCID);
    }
    function testSpecificDisconnect() {
		var o = new TComponent(["retval"]);
		success = true;
		try{
			CA.Components.Register(o);
		}catch(e){
			success = false;
		}
		assertTrue("Should pass due to implementing broadcast interface",success);
		var rCID = null;
		function test(ncid){
			rCID = ncid;
		}
		CA.ComponentCommunications.Connect(o,"event3",test,null);
		CA.ComponentCommunications.FireEvent(o,"event3",o.GetComponentID());
		assertEquals("IDs should equal",o.GetComponentID(),rCID);
		rCID = null;
		CA.ComponentCommunications.Disconnect(o,"event3",test,null);
		assertNotEquals("IDs should not be equal",o.GetComponentID(),rCID);
    }
    function testGeneralDisconnect() {
		var o = new TComponent(["retval"]);
		success = true;
		try{
			CA.Components.Register(o);
		}catch(e){
			success = false;
		}
		assertTrue("Should pass due to implementing broadcast interface",success);
		var rCID = null;
		function test(ncid){
			rCID = ncid;
		}
		CA.ComponentCommunications.Connect(null,"event4",test,null);
		CA.ComponentCommunications.FireEvent(o,"event4",o.GetComponentID());
		assertEquals("IDs should equal",o.GetComponentID(),rCID);
		rCID = null;
		CA.ComponentCommunications.Disconnect(null,"event4",test,null);
		assertNotEquals("IDs should not be equal",o.GetComponentID(),rCID);
    }
  </script>
 </body>
</html>